const fs = require('fs');
const path = require('path');
const homedir = require('os').homedir();

function reslove(fileName = '.kkNoterc') {
  return path.resolve(homedir, fileName);
}

function isHaveFile() {
  return new Promise((resolve, reject) => {
    fs.stat(reslove(), async (err, stats) => {
      if (err) {
        if (err.code === 'ENOENT' || err.errno === -2) {
          resolve(await createFile());
          return;
        }
        resolve(false);
      } else {
        console.log(stats.isFile());
        resolve(stats.isFile());
      }
    });
  });
}

function createFile() {
  return new Promise((resolve, reject) => {
    const configJson = {
      'gitUrl': '',
      'username': '',
      'pwd': '',
      'gitPath': '',
      'gitDir': ''
    };
    fs.writeFile(reslove(), JSON.stringify(configJson), 'utf-8', function (err) {
      if (err) {
        console.log('创建配置文件失败');
        resolve(null);
        return;
      }
      resolve(1);
    });
  });
}
